package rigby.projects.wikipediawebcrawler;

import java.util.Scanner;

public class ConsoleInput {

	Scanner scanInput = new Scanner(System.in);
	
	/**getInt method
	 * 		Scans for keyboard input & loops if catches errors
	 * 
	 * @param getIntMsg
	 * 		String to be used as prompt for user input
	 * @return userInt
	 * 		First valid int entered by the user
	 */
	public int getInt(String getIntMsg, String getIntEndMsg){
		int userInt = 0;
		while (true){
			try{
				System.out.print(getIntMsg);
				userInt = scanInput.nextInt();
				System.out.println(getIntEndMsg);
				
				break;
			} catch (Exception e){
				System.out.println();
				System.out.println("Error: Invalid input");
				System.out.println("Input must be an integer.");
				System.out.println();
				scanInput.nextLine();
			}
		}
		return userInt;
	}

	/**getString method
	 * 		Scans for keyboard input & loops if catches errors
	 * 
	 * @param getStringMsg
	 * 		String to be used as prompt for user input
	 * @return userString
	 * 		First valid String entered by the user
	 */
	public String getString(String getStringMsg, String getStringEndMsg){
		String userString;
		while (true){
			try {
				System.out.print(getStringMsg);
				userString = scanInput.next();
				System.out.println(getStringEndMsg);
				
				break;
			} catch (Exception e){
				System.out.println();
				System.out.println("Error: Invalid Input");
				System.out.println("Input must be a String.");
				scanInput.nextLine();
			}
		}
		return userString;
	}

	/**getValidMenuSelection method
	 * 		Calls getInt for user input & checks validity between params
	 * 		Loops if user input is invalid
	 * 
	 * @param getValidMenuSelectionMsg
	 * 		String to be passed to getInt as prompt
	 * @param menuLowVal
	 * 		int = to the lowest value a user can select in the menu
	 * @param menuHighVal
	 * 		int = to the highest value a user can select in the menu
	 * @return userSelection
	 * 		Valid int entered by the user
	 */
	public int getValidMenuSelection(String getValidMenuSelectionMsg, String getValidMenuSelectionEndMsg, int menuLowVal, int menuHighVal){
		int userSelection = 0;
		while(true){
			userSelection = getInt(getValidMenuSelectionMsg, getValidMenuSelectionEndMsg);
			if(userSelection>=menuLowVal && userSelection<=menuHighVal){
				break;
			}
		}
		return userSelection;
	}

}
